package com.gdufe.firesafe.controller;

import com.gdufe.firesafe.annotation.AuthAccess;
import com.gdufe.firesafe.annotation.NeedAdmin;
import com.gdufe.firesafe.annotation.NeedOwner;
import com.gdufe.firesafe.model.dto.LoginDTO;
import com.gdufe.firesafe.model.dto.RegisterDTO;
import com.gdufe.firesafe.model.dto.ResetDTO;
import com.gdufe.firesafe.model.dto.UpdatePersonalInfoDTO;
import com.gdufe.firesafe.model.entity.UserEntity;
import com.gdufe.firesafe.model.vo.LoginVO;
import com.gdufe.firesafe.service.HouseService;
import com.gdufe.firesafe.service.UserService;
import com.gdufe.firesafe.utils.PageUtils;
import com.gdufe.firesafe.utils.R;
import com.gdufe.firesafe.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * 用户表，用于登录注册，区分用户
 *
 * @author Liu
 * @email
 * @date 2024-03-26 17:30:27
 */
@RestController
@RequestMapping("/user/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private HouseService houseService;

    @AuthAccess
    @GetMapping("/test/hot")
    public String testHot11() {
        return "热部署测试";
    }

    /**
     * 重置密码
     */
    @AuthAccess
    @PutMapping("/resetPassword")
    public R resetPassword(@RequestBody ResetDTO resetDTO){
        userService.resetPassword(resetDTO);
        return R.ok();
    }

    /**
     * 修改用户个人信息
     */
    @PutMapping("/updatePersonalInfo")
    public R updatePersonalInfo(@RequestBody UpdatePersonalInfoDTO updateInfo){
        UserEntity currentUser = TokenUtils.getCurrentUser();
        userService.updatePersonalInfo(updateInfo,currentUser);
        return R.ok();
    }

    /**
     * 获取业主分数
     */
    @NeedOwner
    @GetMapping("/getOwnerPoint")
    public R getOwnerPoint(){
        UserEntity userEntity = TokenUtils.getCurrentUser();
        return R.ok().put("data", houseService.getOwnerPoint(userEntity.getRoleId()));
    }

    /**
     * 验证token
     */
    @GetMapping("/check")
    public R check(){
        return R.ok();
    }

    /**
     *获取当前登录用户个人信息，包括角色相关信息
     */
    @GetMapping("/getPersonalInfo")
    public R getPersonalInfo(){
        UserEntity currentUser = TokenUtils.getCurrentUser();
        return R.ok().put("data",userService.getPersonalInfo(currentUser));
    }

    /**
     * 注册
     */
    @AuthAccess
    @PostMapping("/register")
    public R register(@RequestBody RegisterDTO registerDTO){
        userService.register(registerDTO);
        return R.ok();
    }

    /**
     * 登录
     */
    @AuthAccess
    @PostMapping("/login")
    public R login(@RequestBody LoginDTO loginDTO){
        LoginVO vo=userService.login(loginDTO);
        return R.ok().put("data",vo);
    }

    /**
     * 列表
     */
    @AuthAccess
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		UserEntity user = userService.getById(id);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user){
		userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @NeedAdmin
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @NeedAdmin
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
